﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Hjp.FrameWork.Common;
using Wjtour.Common;


namespace Wjtour.BLL
{
    public class scenepicture_bll : base_dll
    {
        /// <summary>
        /// 获取某个景区的图片
        /// </summary>
        /// <param name="sceneid"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public PaginationData<Wjtour.Domain.sitepicture> GetList(int sceneid, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (sceneid > 0)
                where += " and b.id = " + sceneid;

            string sql = string.Format(@"select c.* from dbo.scenepicture a
            inner join scene b on a.sceneid = b.id
            inner join sitepicture c on a.pictureid= c.id
                {0}
                order by a.id desc", where);
            PaginationData<Wjtour.Domain.sitepicture> data = GetPaginationData<Wjtour.Domain.sitepicture>(sql, page, pagesize);
            return data;
        }

        /// <summary>
        /// 获取景点图片
        /// </summary>
        /// <param name="scenename"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public PaginationData<Wjtour.Domain.dto.sitepicture_scene> GetList(string scenename, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(scenename))
                where += " and b.scenename like '%" + Utility.GetSafeSql(scenename) + "%'";

            string sql = string.Format(@"select c.*,a.sceneid,b.scenename,a.id scenepictureid from dbo.scenepicture a
            inner join scene b on a.sceneid = b.id
            inner join sitepicture c on a.pictureid= c.id
                {0}
                order by a.id desc", where);
            PaginationData<Wjtour.Domain.dto.sitepicture_scene> data = GetPaginationData<Wjtour.Domain.dto.sitepicture_scene>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.scenepicture Load(int id)
        {
            string sql = "select * from scenepicture where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.scenepicture>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.scenepicture entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE scenepicture SET 
                sceneid = @sceneid,
                pictureid = @pictureid,
                addtime = @addtime
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO scenepicture( 
                sceneid,pictureid,addtime)VALUES(
                @sceneid,@pictureid,@addtime)";
            }
            DbAccess.AddInParameter(cmd, "@sceneid", DbType.Int32, entity.sceneid);
            DbAccess.AddInParameter(cmd, "@pictureid", DbType.Int32, entity.pictureid);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;

        }

        /// <summary>
        /// 添加新的景区图片
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="sceneid"></param>
        /// <returns></returns>
        public bool Save(Wjtour.Domain.sitepicture entity, int sceneid)
        {
            string sql = "user_AddScenePicture";
            DbCommand cmd = DbAccess.CreateCommand();
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            DbAccess.AddInParameter(cmd, "@picturetitle", DbType.String, entity.picturetitle);
            DbAccess.AddInParameter(cmd, "@picturedesc", DbType.String, entity.picturedesc);
            DbAccess.AddInParameter(cmd, "@picturetypeid", DbType.Int32, entity.picturetypeid);
            DbAccess.AddInParameter(cmd, "@picturepath", DbType.String, entity.picturepath);
            DbAccess.AddInParameter(cmd, "@linkpath", DbType.String, entity.linkpath);
            DbAccess.AddInParameter(cmd, "@openmode", DbType.String, entity.openmode);
            DbAccess.AddInParameter(cmd, "@sort", DbType.Int32, entity.sort);
            DbAccess.AddInParameter(cmd, "@createdate", DbType.DateTime, entity.createdate);
            DbAccess.AddInParameter(cmd, "@isactive", DbType.Int32, entity.isactive);
            DbAccess.AddInParameter(cmd, "@tag", DbType.String, entity.tag);
            DbAccess.AddInParameter(cmd, "@memberid", DbType.Int32, entity.memberid);
            DbAccess.AddInParameter(cmd, "@isaudit", DbType.Int32, entity.isaudit);
            DbAccess.AddInParameter(cmd, "@smallImg", DbType.String, entity.smallImg);
            DbAccess.AddInParameter(cmd, "@galleryid", DbType.Int32, entity.galleryid);
            DbAccess.AddInParameter(cmd, "@iscover", DbType.Int32, entity.iscover);
            DbAccess.AddInParameter(cmd, "@zan", DbType.Int32, entity.zan);
            DbAccess.AddInParameter(cmd, "@rar", DbType.String, entity.rar);
            DbAccess.AddInParameter(cmd, "@sceneid", DbType.Int32, sceneid);
            DbAccess.AddInParameter(cmd, "@pictureid", DbType.Int32, entity.id);
            return DbAccess.ExecuteNonQuery(cmd) > 0;

        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from scenepicture where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 删除景区管理图片
        /// </summary>
        /// <param name="sceneid"></param>
        /// <param name="picid"></param>
        /// <returns></returns>
        public int Delete(int sceneid, int picid)
        {
            string sql = string.Format("delete from scenepicture where sceneid={0} and pictureid={1}", sceneid, picid);
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
